package com.bifrost.config;

import com.bifrost.task.support.FTPClientConfigure;
import com.bifrost.task.support.FTPClientPool;
import com.bifrost.task.support.FtpClientFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author by yanxiaobo
 * Date on 2018/6/13$.
 */
@Configuration
public class RichBaseFtpConfig {
    Logger logger = LoggerFactory.getLogger(RichBaseFtpConfig.class);

    @Value("${ftp.richBase.server.ip}")
    String ftp_ip;
    @Value("${ftp.richBase.user}")
    String ftp_user;
    @Value("${ftp.richBase.password}")
    String ftp_psw;
    @Value("${ftp.richBase.server.port}")
    int ftp_port;

    @Bean(name = "RichBaseFTPClientPool")
    public FTPClientPool ftpClientPool() {
        FTPClientPool ftpClientPool = null;
        try {
            ftpClientPool = new FTPClientPool(ftpClientFactory());
        } catch (InterruptedException e) {
            logger.error("创建ftpClientPool失败",e);
        }
        return ftpClientPool;
    }

    @Bean(name = "RichBaseFtpClientFactory")
    public FtpClientFactory ftpClientFactory() {
        FtpClientFactory ftpClientFactory = new FtpClientFactory();
        ftpClientFactory.setConfig(fTPClientConfigure());
        return ftpClientFactory;
    }

    @Bean(name = "RichBaseFTPClientConfigure")
    public FTPClientConfigure fTPClientConfigure() {
        FTPClientConfigure ftpClientConfigure = new FTPClientConfigure();

        ftpClientConfigure.setHost(ftp_ip);
        ftpClientConfigure.setUsername(ftp_user);
        ftpClientConfigure.setPassword(ftp_psw);
        ftpClientConfigure.setPort(ftp_port);

        ftpClientConfigure.setPassiveMode("true");
        ftpClientConfigure.setTransferFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClientConfigure.setEncoding("UTF-8");

        return ftpClientConfigure;
    }

}
